package com.ruoyi.spider.processer;

import com.ruoyi.spider.downloader.HttpClientDownloader;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class CompanyProcessor implements PageProcessor {


    private static void trustAllHttpsCertificates() throws Exception {
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
        javax.net.ssl.TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }

    static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }
    }

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(3000)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");


    public void process(Page page) {

        ChromeOptions option = new ChromeOptions();
//是否隐藏浏览器，如果注释掉的话，那么会在启动的时候打开谷歌浏览器，在页面上可以看到填单的操作，如果取消的话，就只有cmd窗口，谷歌浏览器隐匿运行
//            option.addArguments("headless");
//            option.setHeadless(Boolean.TRUE);
//            option.addArguments("window-size=10,10");

        option.addArguments("--no-sandbox");
        option.addArguments("--disable-dev-shm-usage");
        System.setProperty("webdriver.chrome.driver", "/Users/king/Downloads/chromedriver");
//            System.setProperty("webdriver.chrome.driver", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
        WebDriver driver = new ChromeDriver(option);

        try {
            driver.get("https://xiapi.xiapibuy.com/search?keyword=%E6%B4%97%E8%A1%A3%E7%90%83");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


//        String pageResource = driver.getPageSource();
//        WebElement webElement = driver.findElement(By.id("tableData_stockListCompany"));
////        WebElement webElement = driver.findElement(By.xpath("//div[@class='table-responsive sse_table_T05']"));
//        String str = webElement.getAttribute("outerHTML");

        String pageResource = driver.getPageSource();
        System.out.println(pageResource);

        Html html = new Html(pageResource);
        System.out.println(html.xpath("//tbody/tr").all());
        String companyCode = html.xpath("//tbody/tr[1]/td/text()").get();

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = html.xpath("//tbody/tr[3]/td/text()").get().split("/")[0];

        String stockCode = html.xpath("//tbody/tr[2]/td/text()").get().split("/")[0];
        String name = html.xpath("//tbody/tr[5]/td/text()").get().split("/")[0];
        String department = html.xpath("//tbody/tr[14]/td/text()").get().split("/")[0];
        System.out.println(companyCode);
        System.out.println(stockCode);
        System.out.println(name);
        System.out.println(department);
        driver.close();

    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        try {
            trustAllHttpsCertificates();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Spider.create(new CompanyProcessor())
                .addUrl("https://yoopu.me/explore")
                .thread(5).setDownloader(new HttpClientDownloader())
                .run();
    }
}